<?php
class Default_Model_Image {
	protected $_messageError = null;
	protected $_imgPath = 'public/default/upload/place/';
	protected $_imgThumbPath = 'public/default/upload/place/thumb/';
	protected $_imgName = null;
	public function isAllowImg($fileName) {
		$fileExtension = substr ( strrchr ( $fileName, '.' ), 1 );
		$fileExtension = strtolower ( $fileExtension );
		if ($fileExtension == "gif" || $fileExtension == "jpg" || $fileExtension == "png") {
			return true;
		} else {
			return false;
		}
	}
	
	/*
	 * Author(s): Lucky Spadille <spadille.lucky@gmail.com> website: http://my.opera.com/spadille/blog/ @param string directory path, where store file upload @param string override|rename return array An array store file name
	 * Fix bug by SuTV
	 */
	public function uploadMulti($userName,$option = 'rename') {
		
		$dir_upload = UPLOAD_PHOTO_DIRECTORY . 'images/' . $userName . '/' . date ( 'Y-m-d' ) . '/';
		if (! file_exists( $dir_upload)) {
			mkdir ( $dir_upload, 0777, true );
		}
		$upload = new Zend_File_Transfer_Adapter_Http ();
		$upload->setDestination ( $dir_upload );
		$files = $upload->getFileInfo ();
		//var_dump($files);
		$arrFileName = array ();
		$countFileUpload = count ( $files );
		//print_r($countFileUpload);
		if ($countFileUpload > 0) {
			$i = 1;
			foreach ( $files as $file => $info ) {
				if ($info ['name'] != '') {
					$validator = new Zend_Validate_File_Exists ( $dir_upload );
					if ($option == 'rename') {
						if ($validator->isValid ( $info ['name'] )) {
							
							$file_name = $upload->getFileName ( $info ['name'] );
							preg_match ( "/\.([^\.]+)$/", $file_name, $matches );
							$file_ext = $matches [1];
							$currentTime = date ( 'H-i-s' );
							$micro = ( int ) (microtime ( true ) * 1000);
							if ($countFileUpload > 1) {
								$file_name = $currentTime.$micro . '_' . $i . '.' . $file_ext;
							} else {
								$file_name = $currentTime.$micro . '.' . $file_ext;
							}
							$arrFileName [$file] = $file_name;
							$upload->addFilter ( 'Rename', $dir_upload . '/' . $file_name );
							$i ++;
						} else {
							$arrFileName [$file] = $info ['name'];
						}
					} else {
						$arrFileName [$file] = $info ['name'];
					}
					$upload->receive ( $file );
				}
			}
		}
		
		return $arrFileName;
	}
	public function uploadImg($userName) {
		try {
			$strDirectory = UPLOAD_PHOTO_DIRECTORY . 'images/' . $userName . '/' . date ( 'Y-m-d' ) . '/';
			if (! file_exists ( $strDirectory)) {
				mkdir ( $strDirectory, 0777, true );
			}
			
			$upload = new Zend_File_Transfer_Adapter_Http ();
			$info = $upload->getFileName ( 'picture' );
			// preg_match('#\.([^\.]+)$#',$info,$matches);
			// $fileExtension = $matches[1];
			
			$fileExtension = substr ( strrchr ( $info, '.' ), 1 );
			
			$micro = ( int ) (microtime ( true ) * 1000);
			
			$currentTime = date ( 'H-i-s' );
			
			$this->_imgName = $currentTime . $micro . '.' . $fileExtension;
			
			// set img path
			$imgPath = $strDirectory . $this->_imgName;
			
			$this->_imgName = 'images/' . $userName . '/' . date ( 'Y-m-d' ) . '/' . $this->_imgName;
			
			$options = array (
					'target' => $imgPath,
					'overwrite' => true 
			);
			$upload->addFilter ( 'Rename', $options, 'picture' );
			$upload->receive ( 'picture' );
			
			return true;
		} catch ( Exception $e ) {
			$this->_messageError = $e->getMessage ();
			return false;
		}
	}
	public function createThumbImg() {
		try {
			$thumb = VnSpot_PhpThumb_PhpThumbFactory::create ( $this->_imgPath . $this->_imgName );
			$thumb->resize ( 200, 200 );
			$thumb->save ( $this->_imgThumbPath . $this->_imgName );
			return true;
		} catch ( Exception $e ) {
			$this->_messageError = $e->getMessage ();
			return false;
		}
	}
	public function deleteImg($img) {
		$imgPath = UPLOAD_PHOTO_DIRECTORY . $img;
		try {
			if (file_exists ( $imgPath ) && is_readable($imgPath)) {
				unlink ( $imgPath );
			}
			return true;
		} catch ( Exception $e ) {
			return false;
			$this->_messageError = $e->getMessage ();
		}
	}
	public function getImgName() {
		return $this->_imgName;
	}
	public function setImgPath($imgPath) {
		$this->_imgPath = $imgPath;
	}
	public function getImgPath() {
		return $this->_imgPath;
	}
	public function setImgThumbPath($imgThmubPath) {
		$this->_imgThumbPath = $imgThmubPath;
	}
	public function getImgThumbPath() {
		return $this->_imgThumbPath;
	}
	public function getError() {
		return $this->_messageError;
	}
}